
const mimeType={
	css:"text/css",
	htm:"text/html",
	html:"text/html",
	txt:"text/plain",
	js:"text/javascript",
	gif:"image/gif",
	png:"image/png",
	jpeg:"image/jpeg",
	jpg:"image/jpeg",
	mp3:"audio/mpeg",
	m3u8:"audio/x-mpegurl",
	wav:"audio/x-wav",
	bmp:"image/bmp",
	svg:"image/svg+xml",
	mpeg:"video/mpeg",
	mpg:"video/mpeg",
	gz:"application/x-gzip",
	zip:"application/zip"
}
const port=3000;
const http=require("http");
const fs=require("fs");
const { exec } = require('child_process')

http.createServer((req,res)=>{	
	let file=req.url.substr(1) || 'index.html';
	let type=file.split('?')[0].split(".");
	let path=__dirname+'\\'+file;	
	//console.log('path',path,req.url.substr(1))
	let is=fs.existsSync(path);	
	type=type[type.length-1];
	type=mimeType[type] || 'application/octet-stream';
	if(is){
		res.writeHead(200,{'Content-Type':type});
		let fileStream=fs.createReadStream(path);
		fileStream.on('data',data=>{
			res.write(data);
		});
		fileStream.on('end',()=>{			
			res.end();
		})
	}else{
		res.writeHead(404,{'Content-Type':type});		
		res.end();
	}
}).listen(port);

exec(`start chrome http://localhost:${port} -app`);
console.log(`启动成功 server:http://localhost:${port}`);